﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;
using OpenCvSharp.Blob;


namespace RecognitionNumbers
{
    public class Motion : SearchRectangle
    {
        IplImage old;

        public Motion(IplImage image):base (image)
        {
            CvSize size = new CvSize(64, 48);
            old = new IplImage(size, image.Depth, image.NChannels);
            Cv.Resize(image, old, Interpolation.Linear);
      
        }

        public override void search(IplImage image)
        {
            Double shum=0.2;
            CvSize size = new CvSize(64, 48);
            original = image.Clone() ;
            if (old == null)
            {
                coordinatesRectangle_.Clear();
                old = new IplImage(size, original.Depth, original.NChannels);
                Cv.Resize(original, old, Interpolation.Linear);
            }
            else
            {
               
                coordinatesRectangle_.Clear();
                IplImage t = new IplImage(size, original.Depth, original.NChannels);
               
                Cv.Resize(original, t, Interpolation.Linear);
                
                bool[,] r = new bool[size.Height, size.Width];
                for (int i = 0; i < size.Height; i++)
                    for (int j = 0; j < size.Width; j++)
                    {
                        r[i, j] = (Math.Abs(old[i, j].Val0 - t[i, j].Val0) <= old[i, j].Val0 * shum) & (Math.Abs(old[i, j].Val1 - t[i, j].Val1) <= old[i, j].Val1 * shum) & (Math.Abs(old[i, j].Val2 - t[i, j].Val2) <= old[i, j].Val2 * shum) & (Math.Abs(old[i, j].Val3 - t[i, j].Val3) <= old[i, j].Val3 * shum);
                        if (r[i,j])
                        {
                            t[i, j] = new CvScalar(0, 0, 0,0);

                        }
                    }
                old = new IplImage(size, image.Depth, image.NChannels);
                Cv.Resize(image, old, Interpolation.Linear);

                //////////////////////////////////////////////
                Gray сераяКартинка = new Gray(t);
            Binarization чбКартинка = new Binarization(сераяКартинка.output, 10);
        
            IplImage imgLabel = new IplImage(t.Size, BitDepth.F32, 1);
            CvBlobs blobs = new CvBlobs();
            uint result = blobs.Label(чбКартинка.output, imgLabel);
          
            
            foreach (KeyValuePair<uint, CvBlob> item in blobs)
            {
                
                CvBlob b = item.Value;





            

                CvPoint lt = new CvPoint(0, 0);
                CvPoint rt = new CvPoint(0, 0);
                CvPoint rb = new CvPoint(0, 0);
                CvPoint lb = new CvPoint(0, 0);
                lt.X = Convert.ToInt32( b.MinX*(Convert.ToDouble(image.Width) / size.Width));
                lt.Y = Convert.ToInt32(b.MinY* (Convert.ToDouble( image.Height)/size.Height));
                rb.X=Convert.ToInt32(b.MaxX*(Convert.ToDouble(image.Width) / size.Width));
                rb.Y = Convert.ToInt32(b.MaxY * (Convert.ToDouble(image.Height)/size.Height));
                lb.X = lt.X;
                lb.Y = rb.Y;
                rt.Y = lt.Y;
                rt.X = rb.X;
   

                if (((rt.X - lt.X) > 10) & ((rb.X - lb.X) > 10) & ((rb.Y - rt.Y) > 10) & ((lb.Y - lt.Y) > 10) & (b.Rect.Width > t.Size.Width * 0.05) & (b.Rect.Width < t.Size.Width * 0.5) & (b.Rect.Height > t.Size.Height * 0.05) & (b.Rect.Height < t.Size.Height * 0.5))
                {
                    coordinatesRectangle_.Add(new Rectangle(lt, rt, rb, lb));
                }
               
                 
            } 
            
            blobs.Clear();
            blobs = null;

            if (coordinatesRectangle_.Count > 0)
            {
                List<Rectangle> te = new List<Rectangle>();
                foreach (Rectangle o in coordinatesRectangle_)
                {
                    foreach (Rectangle m in coordinatesRectangle_)
                    {
                        if (((m.lt.X > o.lt.X) & (m.lt.Y > o.lt.Y) & (m.lt.X < o.rb.X) & (m.lt.Y < o.rb.Y)))
                        {
                            te.Add(m);
                            break;
                        }
                    }
                }
                foreach (Rectangle o in te)
                {
                    coordinatesRectangle_.Remove(o);
                }
              
               
            }



                ///////////////////////////////////////////////////
            
                transformed = image.Clone();
            
               

                
            }
            
        }


    }
}
